When I started using GitHub, the easy part was finding interesting repositories. The confusing part was working out where my own changes had gone. I’d edited a file and run git add. Why couldn’t I see the change on GitHub yet?

It helped to follow one edit all the way through. The file I type into lives in my working tree. When I stage a change, Git puts that version in the index, ready for my next commit. A commit records those staged changes in the history on my machine. Only when I push does the remote repository get the commit.

These are the commands I wrote down as I went.

Getting a repository onto my machine

On GitHub, a repository is the home for a project. When creating one, I can ask GitHub to initialize it with a README containing the project description. To work on an existing repository locally, I copy its HTTP URL and run:

1
git clone <repository-url>

By default I get a folder named after the repository. Inside it, the hidden .git directory is where Git keeps track of the project. I can put a different local directory name at the end of the command. I can also use -o <name> to give the remote a different name; the default is origin.

When I’m starting with files I already have, git init turns the current folder into a local repository. I still need to connect it to a remote if I want to publish it.

From a file on disk to a commit

For a quick experiment, echo 'text' prints a line in the terminal. Add > filename to write it to a file, or >> filename to append it. Now there’s an edit for Git to see.

  1. Run git status to see what’s changed.
  2. Use git add <file> to stage one file or git add . to stage changes under the current directory.
  3. Check git diff --cached. This is the diff the next commit will actually contain.
  4. Run git commit -m 'describe the change' to record it. Check git log to see it in the project history. Add --reverse to read from the oldest commit forward.

Staged the wrong edit? git reset -- <file> takes the change out of the staging area and leaves the edited file on disk. I also kept seeing git rm --cached <file>: that leaves the file on disk too, but stages its removal from version control. The confusing terminology is easier once you realize “staging area,” “index,” and “cache” refer to the same part of Git here.

Moving back through local history

After a commit, git reset --soft HEAD^ moves the branch back one commit while keeping its changes staged. For two commits, use HEAD^^; for n, HEAD~n.

git reflog is handy when I know where I was a few operations ago but have forgotten the commit ID. It lists earlier positions of the local references. I can name one of those positions as HEAD@{n}. A hard reset, git reset --hard <revision>, takes the working tree and index back as well. That difference matters if I still want the edits on disk.

Working on a branch and sharing it

A team might keep its everyday development, beta, and release work on different branches. git branch -avv shows both local and remote-tracking branches, with enough detail to see how they relate.

  • git branch dev1 creates a local branch. git checkout dev1 switches to it. I can do both at once with git checkout -b dev1.
  • git push origin dev1 sends it to the remote named origin and creates the remote branch if needed. To give the remote branch a different name, use git push <remote> <local-name>:<remote-name>.
  • git push -u origin dev1 also remembers which remote branch this local branch tracks. If the remote branch already exists, I can set that relationship with git branch -u origin/dev1. After that, a plain git push generally does what I want. The -f flag forces the update.
  • git fetch retrieves what’s changed on the remote. In this repository the main branch was called master, so I could follow a fetch with git rebase origin/master. git pull fetches and integrates remote changes in one go.
  • git branch -m <old> <new> renames a local branch. git branch -D <name> deletes a local branch; git push origin --delete <name> deletes the branch on the remote.

Putting a name on a release

A branch moves as development continues. A tag gives a particular revision a name people can come back to, like a release number. On GitHub people can download that tagged snapshot as a ZIP or a tar.gz.

1
2
3
git tag <tag> -m 'release notes' [revision]
git show <tag>
git push origin <tag>

Leave out the revision to tag the current commit. I like to include a message. To publish all local tags together, use git push origin --tags. To remove a tag locally, use git tag -d <tag>; the remote has its own copy, which I can remove with git push origin :refs/tags/<tag>.

A few shortcuts I kept

git config --global alias.st status lets me type git st. I set br to 'branch -avv' the same way and ch to checkout. When I forget which aliases I’ve set, git config -l lists them.